package com.firemoss.atonement.mapper.resolution
{
	import com.firemoss.atonement.mapper.Mapping;
	import com.firemoss.atonement.metadata.Property;
	
	import flash.utils.Dictionary;
	import com.firemoss.atonement.mapper.Mapping;
	
	public class MappedValueResolver implements IValueResolver
	{
		public function MappedValueResolver()
		{
		}

		public static function resolve(source:*, targetProperty : Property, mapping:Mapping, mappingChain:Dictionary):*
		{
			if ( source == null )
			{
				return null
			}
			else if ( mappingChain[ source ] == null )
			{
				var result : * = mapping.mapper.graphedInstanceFactory.createByTargetProperty( source, targetProperty )
					
				mapping.mapper.map( source, result, mappingChain );
					
				mappingChain[ source ] = result
				
				return result
			}
			else
			{
				return mappingChain[ source ]
			}
		}
		
		public function resolve(source:*, targetProperty : Property, mapping:Mapping, mappingChain:Dictionary):*
		{
			return MappedValueResolver.resolve( source, targetProperty, mapping, mappingChain );
		}
	}
}